// components/league-member/index.js
import api from '../../../api/index'
Component({
  lifetimes: {
    attached: function () {
      console.log('团员.........')
      // 在组件实例进入页面节点树时执行

    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    teamInfo: {}, //团队信息
    date: '',
    earnings: {}, //收益
    rank: {}, //排名
    grade: 0, //当前等级
    list: [], //订单列表
    isLoading: false,
    isComplete: false,
    pageNo: 1
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init() {
      this.getTeamInfo();
      let date = this.formatTime(new Date());
      this.setData({
        date: date.year + '-' + date.month,
        list: [], //订单列表
        isLoading: false,
        isComplete: false,
        pageNo: 1
      })
      this.getGross();
      this.getRank();
      this.getGrade();
      this.getMemberOrder();
    },
    //申请或取消
    applicationOrCancellation(e) {
      let type = e.currentTarget.dataset.type;
      wx.showModal({
        title: '提示',
        content: '是否确认此操作?',
        success: (res) => {
          if (res.confirm) {
            if (type == 'exit') {
              //退团
              api.appleExit().then(() => {
                wx.showToast({
                  title: '操作成功',
                })
                this.triggerEvent('exit');//抛出事件
              })
            }else if(type == 'sinceGroup'){
              //退团并组建
              wx.navigateTo({
                url: '/pages/since-group-team/index',
              })
            }
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })

    },
   
    //下拉触底
    lower() {
      !this.data.isComplete && !this.data.isLoading && this.getMemberOrder();
    },
    //获取订单信息
    getMemberOrder() {
      this.setData({
        isLoading: true
      })
      api.getMemberOrder({
        pageNo: this.data.pageNo
      }).then(res => {
        this.setData({
          list: this.data.list.concat(res.data.data.records),
          isLoading: false,
          isComplete: !res.data.data.records.length,
          pageNo: ++this.data.pageNo
        })
      })
    },
    //获取当前等级
    getGrade() {
      api.getGrade().then(res => {
        this.setData({
          grade: res.data.data
        })
      })
    },

    //获取排名
    getRank() {
      api.getRank().then(res => {
        this.setData({
          rank: res.data.data
        })
      })
    },
    //选项时间
    bindDateChange(e) {
      this.setData({
        date: e.detail.value
      })
      this.getGross();
    },
    //格式化时间
    formatTime(time) {
      let val = new Date(time);
      let date = {
        year: val.getFullYear(),
        month: val.getMonth() + 1
      }
      return date;
    },
    //获取团队信息
    getTeamInfo() {
      api.getTeamInfo().then(res => {
        this.setData({
          teamInfo: res.data.data
        })
      })
    },
    //获取收入
    getGross() {
      api.getGross({
        date: this.data.date
      }).then(res => {
        this.setData({
          earnings: res.data.data
        })
      })
    },

  }
})